package com.jdkreader.concurrency.concurrent.semaphore;

import java.util.concurrent.TimeUnit;

/**
 * 从对象池检出资源的任务
 */
public class CheckOutTask<T> implements Runnable{

    private static int counter = 0;
    private final int id = counter++;
    private Pool<T> pool;
    public CheckOutTask(Pool<T> p){
        pool=p;
    }

    @Override
    public void run() {
        try{
            T item=pool.checkOut();
            System.out.println(this+" checked out "+item);
            TimeUnit.SECONDS.sleep(1);
            System.out.println(this+" checked in "+item);
            pool.checkIn(item);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

    public String toString(){
        return "CheckOutTask "+id+" ";
    }
}
